<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <div class="search-title">快速查询</div>
          <el-input v-model.trim="txtSearch" @input="btnQuery" placeholder="架子类型代码|名称" style="width:120px" :disabled="changeFlag"></el-input>
          <div class="search-title">样本库</div>
          <el-select v-model="selCodeno" filterable style="width:120px" @change="libInit" ref="refSelCodeno" :disabled="changeFlag" >
              <el-option v-for="(item, index) in selSmpLibrary_Options" :key="index" :value="item.librid" :label="item.librname" ></el-option>
          </el-select>
          <div class="search-title">架子类型</div>
          <el-select v-model="selShelfType" style="width:120px" @change="btnQuery" placeholder="请选择" clearable no-data-text="当前样本库下无数据" :disabled="changeFlag">
              <el-option v-for="item in selShelfidOptions" :key="item.shelftypeno" :label="item.shelftypename" :value="item.shelftypeno">
              </el-option>
          </el-select>
          <div class="search-title">使用状态</div>
          <el-select v-model="selUsestatus" style="width:120px" @change="btnQuery" placeholder="请选择" :disabled="changeFlag">
              <el-option v-for="item in selUsestatusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
          </el-select>
          <div class="top-deal">
              <el-button @click="btnQuery()" icon="el-icon-search" v-has="'PINQUERY'" type="primary">查询</el-button>
              <el-button icon="el-icon-plus" v-has="'PADD'" @click.native="btnAdd()" :disabled="changeFlag">新增</el-button>

              <el-button icon="el-icon-delete" v-has="'PDELETE'" @click.native="btnDelete()" :disabled="changeFlag">删除</el-button>

              <el-button icon="el-icon-document" v-has="'PMODIFY'" @click.native="btnSave()" :disabled="!changeFlag">保存</el-button>

              <el-button icon="el-icon-document" v-has="'PMODIFY'" @click.native="btnCancel()" :disabled="!changeFlag">取消</el-button>
          </div>
      </div>
      <div class="pagecenter">
          <rm-layout-right splitcolor="#FFFFFF" movingcolor="#CCFFFF" :splitwidth=5 :fixsize=400>
              <vxe-table :loading="loading" :data.sync="leftTableData" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row ref="xTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe @current-change="CurrentChange" :cell-class-name="cellClassName" :row-class-name="RowClassName">
                  <vxe-table-column field="librname" title="样本库" width="80" align="center"></vxe-table-column>
                  <vxe-table-column field="shelftypename" title="架子类型" width="100px"></vxe-table-column>
                  <vxe-table-column field="shelfno" title="架子编号" width="100px"></vxe-table-column>
                  <vxe-table-column field="shelfdesc" title="架子说明" width="200px"></vxe-table-column>
                  <vxe-table-column field="rownumber" title="总行数" width="40px" align="center"></vxe-table-column>
                  <vxe-table-column field="colnumber" title="总列数" width="40px" align="center"></vxe-table-column>
                  <vxe-table-column field="posnumber" title="总位置数" width="60px" align="center"></vxe-table-column>
                  <vxe-table-column field="occupynumber" title="使用位置数" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="freenumber" title="空闲位置数" width="80px" align="center"></vxe-table-column>
                  <vxe-table-column field="locationna" title="所处库位" header-align="center" align="left" width="160px"></vxe-table-column>
                  <vxe-table-column field="usestatus" title="使用状态" width="80px" align="center" :formatter="['formatSelect', selUsestatusOptions]"></vxe-table-column>
                  <vxe-table-column field="createuser" title="创建人" width="60px" align="center"></vxe-table-column>
                  <vxe-table-column field="createdt" title="创建时间" width="120px" align="center"></vxe-table-column>
                  <vxe-table-column field="shelfid" title="样本架ID" header-align="center" align="center" width="60px"></vxe-table-column>
              </vxe-table>
              <template v-slot:fix>
                  <div style="width:100%;float:left;height:100%;border:1px solid #ccc;overflow:auto;" id="showbox">
                      <el-form :inline="true" @submit.native.prevent label-width="auto" ref="form_bbjlx" :model="detailData" :key="itemKey">
                          <table style="width:100%;font-size:12px;white-space: nowrap; padding-top:3px;text-align: left; align:;center;padding:0px 5px 5px 0px">
                              <tr>
                                  <td style="text-align: right; ">样本架ID</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.shelfid" @input="IsChange" ref="txtshelfid" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right;color:#F56C6C;">架子类型</td>
                                  <td colspan="3">
                                      <el-select v-model="detailData.shelftypeno" style="width:250px" placeholder="" ref="selShelftypeno" @keyup.enter.native="Autofocus('selShelftypeno')" @change="GetShelfData()" no-data-text="样本库下无数据" clearable :disabled="(!bCanSel&&!bNulladd)||bHaveSmp">
                                          <el-option v-for="item in selDetailshOptions" :key="item.shelftypeno" :label="item.shelftypename" :value="item.shelftypeno">
                                          </el-option>
                                      </el-select>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right;">架子编号</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.shelfno" @input="IsChange" ref="txtShelfno" @keyup.enter.native="Autofocus('txtShelfno')" placeholder="架子编号不可重复" :disabled="!bNulladd||bHaveSmp"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; vertical-align:top;padding-left: 4px;">架子说明</td>
                                  <td colspan="3">
                                      <el-input :rows="3" style="width:250px;" type="textarea" v-model="detailData.shelfdesc" @input="IsChange" ref="txtShelfdesc" @keyup.enter.native="Autofocus('txtShelfdesc')" :disabled="!bNulladd"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">样本库</td>
                                  <td colspan="3">
                                      <el-select v-model="detailData.librid" filterable style="width:250px" ref="selLibrID" @keyup.enter.native="Autofocus('selLibrID')" @change="IsChange" :disabled="true" placeholder="">
                                          <el-option v-for="(item, index) in selSmpLibrary_Options" :key="index" :value="item.librid" :label="item.librname"></el-option>
                                      </el-select>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">总行数</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.rownumber" ref="txtRowNumber" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">总列数</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.colnumber" ref="txtColNumber" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">总位置数</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.posnumber" @input="IsChange" ref="txtPosNumber" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">使用位置数</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.occupynumber" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">空闲位置数</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.freenumber" @input="IsChange" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">所属库位</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.locationna" @input="IsChange" ref="txtLocationID" :disabled="true"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right;">使用状态</td>
                                  <td colspan="3">
                                      <el-select v-model="detailData.usestatus" style="width:250px" @change="btnQuery" placeholder="" @input="IsChange" ref="txtUseStatus" :disabled="true">
                                          <el-option v-for="item in selUsestatusOptions" :key="item.value" :label="item.label" :value="item.value">
                                          </el-option>
                                      </el-select>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">创建人</td>
                                  <td colspan="3">
                                      <rm-ddl-common datatype="userinfo" dataname="用户" v-model="detailData.createuser" width="250px" :disabled="true"></rm-ddl-common>
                                  </td>
                              </tr>
                              <tr>
                                  <td style="text-align: right; ">创建时间</td>
                                  <td colspan="3">
                                      <el-input style="width:250px;" v-model="detailData.createdt" @input="IsChange" :disabled="true"></el-input>
                                  </td>
                              </tr>
                          </table>
                      </el-form>
                  </div>
              </template>
          </rm-layout-right>
      </div>
  </div>
</template>

<script type="text/javascript">
import $ from "jquery";
import { Promise } from "q";
import XE from "xe-utils";
XE.mixin({
  // 自定义全局的格式化处理函数
  formatSelect(cellValue, list) {
      let item = list.find((item) => item.value == cellValue);
      return item ? item.label : "";
  },
});
export default {
  name: "SMP121",
  data() {
      return {
          loading: false,
          bCanSel: false,
          bNulladd: false,
          bHaveSmp: false,
          changeFlag: false,
          okFirstLoading: true,
          tempindex: 0,
          selflag: "1",
          itemKey: 0,
          txtSearch: "", //查询内容（对 样本库代码、名称进行模糊查询）
          selCodeno: "", //样本库类型代码
          selShelfType: "",
          shelfid: "",
          librid: "", //样本库ID
          selUsestatus: "",
          tmpDt: "",
          actionindex: "M", //M默认 A新增 E修改 D删除
          detailData: {
              shelfid: null, //样本架ID"
              librid: null, //样本库ID"
              shelftypeno: "", //架子类型代码"
              shelfno: "", //架子编号"
              shelfdesc: "", //架子说明"
              rownumber: null, //总行数"
              colnumber: null, //总列数"
              posnumber: null, //总位置数"
              occupynumber: null, //已占用位置数
              freenumber: null, //空余位置数
              locationid: null, //所处库位"
              locationna: "",
              usestatus: "", //使用状态"
              createuser: "", //创建人"
              createdt: "", //创建时间"
          },
          oldDetailData: {
              shelfid: null, //样本架ID"
              librid: null, //样本库ID"
              shelftypeno: "", //架子类型代码"
              shelfno: "", //架子编号"
              shelfdesc: "", //架子说明"
              rownumber: null, //总行数"
              colnumber: null, //总列数"
              posnumber: null, //总位置数"
              occupynumber: null, //已占用位置数
              freenumber: null, //空余位置数
              locationid: null, //所处库位"
              locationna: "",
              usestatus: "", //使用状态"
              createuser: "", //创建人"
              createdt: "", //创建时间"
          },
          selRow: {
              shelfid: null, //样本架ID"
              librid: null, //样本库ID"
              shelftypeno: "", //架子类型代码"
              shelfno: "", //架子编号"
              shelfdesc: "", //架子说明"
              rownumber: null, //总行数"
              colnumber: null, //总列数"
              posnumber: null, //总位置数"
              locationid: null, //所处库位"
              locationna: "",
              usestatus: "", //使用状态"
              createuser: "", //创建人"
              createdt: "", //创建时间"
          },
          addData: [],
          modData: [],
          leftTableData: [],
          selShelfidOptions: [],
          selDetailshOptions: [],
          selUsestatusOptions: [
              //0未使用(冰箱内)，1全部使用，2部分使用，3闲置(冰箱外)  空全部使用
              {
                  value: "",
                  label: "全部",
              },
              {
                  value: "0",
                  label: "未使用",
              },
              {
                  value: "1",
                  label: "全部使用",
              },
              {
                  value: "2",
                  label: "部分使用",
              },
              {
                  value: "3",
                  label: "闲置",
              },
          ],
          selSmpLibrary_Options: [], //样本库类型
      };
  },
  watch: {},
  created() {
      this.ddlSmp_Library(); //初始化样本库列表
  },
  mounted() {
      let modOption = localStorage.getItem("selCodenoList");
      let tempOption = JSON.parse(modOption);
      if (modOption) {
          this.selCodeno = tempOption.selCodeno;
      }
      // this.$nextTick(() => {
      //     this.InitPage()
      // })
  },
  updated() {},
  methods: {
      /*-----------------------------初始化-----------------------------*/
      LoadSave() {
          let jsondata = { selCodeno: this.selCodeno };
          let modListJson = JSON.stringify(jsondata);
          localStorage.setItem("selCodenoList", modListJson);
          // this.$nextTick(() => {
          //     this.InitPage()
          // })
      },
      //样本库列表下拉框初始化
      ddlSmp_Library() {
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "q_smp_library",
                  },
              })
              .then((res) => {
                  this.selSmpLibrary_Options = res;
                  // if (this.selSmpLibrary_Options.length > 0) this.selCodeno = this.selSmpLibrary_Options[0].librid;
                  this.ddlShelfId();
              });
      },
      //架子类型下拉框初始化 需要先选择样本库
      ddlShelfId() {
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP120",
                  method: "post",
                  data: {
                      action: "q_smp_shelftype",
                      librid: this.selCodeno,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      this.selShelfidOptions = res.dt; // 用于 top 架子类型下拉查询
                      this.selDetailshOptions = res.dt;
                      this.btnQuery();
                  } else {
                      this.leftTableData = [];
                      this.detailData = {
                          shelfid: null, //样本架ID"
                          librid: null, //样本库ID"
                          shelftypeno: "", //架子类型代码"
                          shelfno: "", //架子编号"
                          shelfdesc: "", //架子说明"
                          rownumber: null, //总行数"
                          colnumber: null, //总列数"
                          posnumber: null, //总位置数"
                          occupynumber: null, //已占用位置数
                          freenumber: null, //空余位置数
                          locationid: null, //所处库位"
                          locationna: "",
                          usestatus: "", //使用状态"
                          createuser: "", //创建人"
                          createdt: "", //创建时间"
                      };
                      this.selShelfidOptions = [];
                      this.selDetailshOptions = [];
                      this.$refs.refSelCodeno.blur();
                      // if (this.selCodeno != "") {
                      //     this.$confirm(
                      //         res.errormsg,
                      //         "提示",
                      //         { type: "warning" },
                      //         {
                      //             confirmButtonText: "立即维护",
                      //             cancelButtonText: "取消",
                      //         }
                      //     )
                      //         .then(() => {
                      //             this.$router.push("/SMP120");
                      //         })
                      //         .catch(() => {
                      //             this.$message({
                      //                 type: "info",
                      //                 message: "已取消跳转，稍后您可以手动跳转至 SMP120 样本架类型 页面进行维护。",
                      //                 showClose: true,
                      //             });
                      //         });
                      // }
                  }
              });
      },
      /*-----------------------------行改变-----------------------------*/
      //行选中
      CurrentChange({ row, rowIndex }, event) {
          this.tempindex = rowIndex;
          if (this.selflag == "1") {
              this.$nextTick(async () => {
                  await this.CurrentChangeEvent(row, rowIndex);
              });
          } else {
              this.$refs.xTable.setCurrentRow(this.selRow);
              return false;
          }
      },
      //行选中事件
      async CurrentChangeEvent(row, rowIndex) {
          this.shelfid = row.shelfid;
          this.selflag = "0";
          this.CurrentSave()
              .then(() => {
                  this.selRow = row;
                  this.shelfid = row.shelfid;
                  this.$refs.xTable.setCurrentRow(row);
                  this.$nextTick(async () => {
                      await this.DetailQuery();
                      this.selflag = "1";
                  });
              })
              .catch((ex) => {
                  this.$refs.xTable.setCurrentRow(this.selRow);
                  this.selflag = "1";
              });
      },
      //换行保存 (新增/修改)
      async CurrentSave() {
          return new Promise((s, f) => {
              let haschange = false;
              let saveinfo = XE.clone(this.detailData, true);
              if (this.actionindex == "M") {
                  //如果是修改
                  if (!XE.isEqualWith(this.oldDetailData, saveinfo)) {
                      //表单被更改
                      haschange = true;
                  }
              } else if (this.actionindex == "A") {
                  //如果是新增
                  saveinfo.createuser = this.$tool.getSession(this.$tool.loginuserno);
                  saveinfo.createdt = this.$pub.InitDateTime();
                  haschange = true;
              }
              if (saveinfo.shelftypeno == "") {
                  this.$alert("架子类型不可为空！", "提示", {
                      confirmButtonText: "确定",
                      type: "warning",
                  });
                  f();
                  this.$refs.selShelftypeno.focus();
                  return false;
              }
              if (saveinfo.librid == "") {
                  this.$alert("样本库ID不可为空！", "提示", {
                      confirmButtonText: "确定",
                      type: "warning",
                  });
                  f();
                  this.$refs.selLibrID.focus();
                  return false;
              }
              if (saveinfo.shelfid == null) {
                  saveinfo.shelfid = 0;
              } else {
                  saveinfo.shelfid = parseInt(saveinfo.shelfid, 10);
              }
              if (saveinfo.locationid == null) {
                  saveinfo.locationid = 0;
              } else {
                  saveinfo.locationid = parseInt(saveinfo.locationid, 10);
              }
              if (haschange) {
                  this.$nextTick(async () => {
                      const res = await this.$tool.request({
                          url: "SMP/SMP1/SMP121",
                          method: "post",
                          data: {
                              action: "smp_shelf_addedit",
                              data: saveinfo,
                              librid: this.selCodeno,
                          },
                      });
                      if (res.errcode == "1") {
                          this.$message({
                              message: "保存成功！",
                              type: "success",
                              showClose: true,
                          });
                          this.shelfid = res.keyid;
                          this.changeFlag = false;
                          //不是删除操作
                          if (this.actionindex == "A") {
                              //新增数据成功后的不刷新操作
                              saveinfo.shelfid = this.shelfid;
                              this.detailData.shelfid = this.shelfid;
                              this.selSmpLibrary_Options.forEach((item) => {
                                  if (saveinfo.librid == item.librid) {
                                      saveinfo.librname = item.librname;
                                  }
                              });
                              saveinfo.freenumber = saveinfo.posnumber;
                              saveinfo.occupynumber = 0;
                              saveinfo.createuser = this.$tool.getSession(this.$tool.loginusername);
                              this.$refs.xTable.insertAt(saveinfo, -1).then(({ row }) => {
                                  this.$refs.xTable.setCurrentRow(row);
                                  this.selRow = row;
                              });
                              if (this.addData.indexOf(this.shelfid) < 0) {
                                  this.addData.push(this.shelfid);
                              }
                          } else if (this.actionindex == "M") {
                              this.setValueToTable(saveinfo);
                              if (this.modData.indexOf(this.shelfid) < 0) {
                                  this.modData.push(this.shelfid);
                              }
                          }
                          this.actionindex = "M";
                          s();
                      } else {
                          this.$message({
                              message: "保存失败！" + res.obj,
                              type: "error",
                              showClose: true,
                          });
                          f();
                      }
                  });
              } else {
                  this.changeFlag = false;
                  s();
              }
          });
      },
      /*-----------------------------按钮类-----------------------------*/
      //查询主表
      async btnQuery() {
          this.actionindex = "M";
          if (this.selCodeno == "") {
              this.$message({
                  message: "查询数据前，请先选择所属的样本库。",
                  type: "warning",
                  showClose: true,
              });
              this.$refs.refSelCodeno.focus();
              return false;
          } else {
              this.loading = true;
              this.addData = [];
              this.modData = [];
              try {
                  const res = await this.$tool.request({
                      url: "SMP/SMP1/SMP121",
                      method: "post",
                      data: {
                          action: "smp_shelf_query",
                          keystr: this.txtSearch,
                          librid: this.selCodeno,
                          shelftypeno: this.selShelfType,
                          usestatus: this.selUsestatus,
                      },
                  });
                  console.log(" res.RData.Data", res.RData.Data);
                  this.leftTableData = res.RData.Data;
                  this.leftTableData.forEach((item) => {
                      this.selSmpLibrary_Options.forEach((item2) => {
                          if (item.librid == item2.librid) {
                              item.librname = item2.librname;
                          }
                      });
                  });
                  this.okFirstLoading = true;
                  this.selRow = null;
                  if (res.RData.Data.length > 0) {
                      this.bNulladd = true;
                      this.selRow = res.RData.Data[0];
                      this.shelfid = res.RData.Data[0].shelfid;
                      this.librid = res.RData.Data[0].librid;
                      this.$refs.xTable.setCurrentRow(this.leftTableData[0]);
                      this.shelftypeno = this.leftTableData[0].shelftypeno;
                  } else {
                      this.shelfid = "";
                      this.librid = "";
                      this.bNulladd = false;
                      this.detailData = {
                          shelfid: null,
                          librid: null,
                          shelftypeno: "",
                          shelfno: "",
                          shelfdesc: "",
                          rownumber: null,
                          colnumber: null,
                          posnumber: null,
                          occupynumber: null, //已占用位置数
                          freenumber: null, //空余位置数
                          locationid: null,
                          locationna: "",
                          usestatus: "0",
                          createdt: "",
                          createuser: "",
                      };
                  }
                  if (this.shelfid != "" && this.selCodeno != "") {
                      await this.DetailQuery();
                  }
                  this.loading = false;


                  this.changeFlag = false;

              } catch (e) {
                  this.loading = false;
                  this.$message({
                      message: e.message,
                      type: "error",
                      showClose: true,
                  });
              }
          }
      },
      //查询明细
      async DetailQuery() {
          this.bCanSel = false;
          try {
              const res = await this.$tool.request({
                  url: "SMP/SMP1/SMP121",
                  method: "post",
                  data: {
                      action: "smp_shelf_query_byid",
                      shelfid: this.shelfid,
                  },
              });
              this.detailData = res.Data[0];
              this.oldDetailData = XE.clone(this.detailData, true);
              if (this.detailData.usestatus == "0" || this.detailData.usestatus == "1" || this.detailData.usestatus == "2") {
                  this.bHaveSmp = true;
              } else {
                  this.bHaveSmp = false;
              }
          } catch (e) {
              this.$message({
                  message: e.message,
                  type: "error",
                  showClose: true,
              });
          }
      },
      //新增
      btnAdd() {
          if (this.selCodeno == "") {
              this.$message({
                  message: "新增数据前，请先选择所属的样本库。",
                  type: "warning",
                  showClose: true,
              });
              this.$refs.refSelCodeno.focus();
              return false;
          } else {
              this.actionindex = "A";
              this.changeFlag = true;
              // this.itemKey++
              this.bCanSel = true;
              this.bNulladd = true;
              this.bHaveSmp = false;
              this.$refs.selShelftypeno.focus();
              this.detailData = {
                  shelfid: null,
                  librid: null,
                  shelftypeno: "",
                  shelfno: "",
                  shelfdesc: "",
                  rownumber: null,
                  colnumber: null,
                  posnumber: null,
                  occupynumber: null, //已占用位置数
                  freenumber: null, //空余位置数
                  locationid: null,
                  locationna: "",
                  usestatus: "0",
                  createdt: "",
                  createuser: this.$tool.getSession(this.$tool.loginuserno),
              };
          }
      },
      //删除
      btnDelete() {
          let that = this;
          if (this.$refs.xTable.tableData.length < 1) {
              this.$message({
                  message: "请先选择一条数据！",
                  type: "error",
                  showClose: true,
              });
              return false;
          }
          if (this.bHaveSmp) {
              this.$alert("当前样本架已使用。", "提示", {
                  confirmButtonText: "确定",
                  type: "warning",
              });
              return false;
          }
          this.$confirm("你确定删除这笔记录?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
          })
              .then(() => {
                  this.$tool
                      .request({
                          url: "SMP/SMP1/SMP121",
                          method: "post",
                          data: {
                              action: "smp_shelf_delete",
                              shelfid: this.shelfid,
                          },
                      })
                      .then((res) => {
                          if (res == "1") {
                              this.$message({
                                  message: "删除成功！",
                                  type: "success",
                                  showClose: true,
                              });
                              this.actionindex = "M";
                              //是删除操作
                              var delrow = that.$refs.xTable.getCurrentRow();

                              if (delrow) {
                                  var alld = that.$refs.xTable.getTableData().fullData;
                                  var rowindex = alld.indexOf(delrow);
                                  that.$refs.xTable.remove(delrow);
                                  setTimeout(async () => {
                                      let havedata = that.$refs.xTable.tableData;
                                      if (havedata.length > 0) {
                                          if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                                          let selectrow = havedata[rowindex];
                                          that.$refs.xTable.setCurrentRow(selectrow);
                                          that.shelfid = selectrow.shelfid;
                                          await this.DetailQuery();
                                          that.$refs.xTable.scrollToRow(selectrow);
                                      }
                                  }, 5);
                              }
                          } else {
                              this.$message({
                                  message: "删除失败！" + res,
                                  type: "error",
                                  showClose: true,
                              });
                          }
                      })
                      .catch((e) => {
                          this.$message({
                              message: e,
                              type: "error",
                              showClose: true,
                          });
                      });
              })
              .catch(() => {
                  this.$message({
                      type: "info",
                      message: "已取消删除",
                      showClose: true,
                  });
              });
      },
      //保存 (新增/修改)
      btnSave() {
          let that = this;
          let gData = XE.clone(that.detailData, true);
          if (this.actionindex == "A") {
              gData.createuser = this.$tool.getSession(this.$tool.loginuserno);
              gData.createdt = this.$pub.InitDateTime();
              this.tmpDt = gData.createdt;
          } else if (this.actionindex == "M") {
              //无数据时
              if (this.shelfid == "") {
                  this.$alert(
                      "请通过新增按钮来添加数据。",
                      "提示",
                      { type: "warning" },
                      {
                          confirmButtonText: "确定",
                      }
                  );
                  this.btnCancel();

                  return false;
              }
          }
          if (gData.shelftypeno == "") {
              this.$alert(
                  "架子类型不可为空！",
                  "提示",
                  { type: "warning" },
                  {
                      confirmButtonText: "确定",
                  }
              );
              this.$refs.txtShelfTypeName.focus();
              return false;
          }
          if (gData.librid == "") {
              this.$alert(
                  "样本库ID不可为空！",
                  "提示",
                  { type: "warning" },
                  {
                      confirmButtonText: "确定",
                  }
              );
              this.$refs.selLibrID.focus();
              return false;
          }
          if (gData.shelfid == null) {
              gData.shelfid = 0;
          } else {
              gData.shelfid = parseInt(gData.shelfid, 10);
          }
          if (gData.locationid == null) {
              gData.locationid = 0;
          } else {
              gData.locationid = parseInt(gData.locationid, 10);
          }
          this.loading = true;
          this.changeFlag = true;
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP121",
                  method: "post",
                  data: {
                      action: "smp_shelf_addedit",
                      data: gData,
                      librid: this.selCodeno,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      that.$message({
                          message: "保存成功！",
                          type: "success",
                          showClose: true,
                      });
                      this.shelfid = res.keyid;
                      this.loading = false;
                      //不是删除操作
                      if (this.actionindex == "A") {
                          gData.shelfid = this.shelfid;
                          gData.freenumber = gData.posnumber;
                          gData.occupynumber = 0;
                          gData.usestatus = 3;
                          this.detailData.createdt = this.tmpDt;
                          this.selSmpLibrary_Options.forEach((item) => {
                              if (gData.librid == item.librid) {
                                  gData.librname = item.librname;
                              }
                          });
                          this.selDetailshOptions.forEach((item) => {
                              if (gData.shelftypeno == item.shelftypeno) {
                                  gData.shelftypename = item.shelftypename;
                              }
                          });
                          gData.createuser = this.$tool.getSession(this.$tool.loginusername);
                          this.detailData.shelfid = this.shelfid;
                          this.$refs.xTable.insertAt(gData, -1).then(({ row }) => {
                              this.$refs.xTable.setCurrentRow(row);
                              this.selRow = row;
                          });
                          if (this.addData.indexOf(this.shelfid) < 0) {
                              this.addData.push(this.shelfid);
                          }
                      } else if (this.actionindex == "M") {
                          this.setValueToTable(gData);
                          if (this.modData.indexOf(this.shelfid) < 0) {
                              this.modData.push(this.shelfid);
                          }
                          var mrow = this.$refs.xTable.getCurrentRow();
                          this.$refs.xTable.setCurrentRow(mrow);
                      }
                      this.selRow = this.$refs.xTable.getCurrentRow();
                      this.actionindex = "M";
                      setTimeout(async () => {
                          if (this.selRow) {
                              await this.DetailQuery(); //查后台记录当前的值方便比较保存后值是否发生改变
                              this.$refs.xTable.setCurrentRow(this.selRow);
                              this.$refs.xTable.scrollToRow(this.selRow);
                          }
                      }, 5);
                  } else {
                      this.loading = false;
                      this.actionindex = that.actionindex;
                      console.log("res", res);
                      this.$message({
                          message: "保存失败！" + res.obj,
                          type: "error",
                          showClose: true,
                      });
                  }
              })
              .catch((e) => {
                  this.loading = false;
                  that.$message({
                      message: e,
                      type: "error",
                      showClose: true,
                  });
              });
      },
      //取消
      async btnCancel() {
          this.actionindex = "M";
          this.selflag = "1";
          this.changeFlag = false;
          this.bCanSel = false;
          let row = this.$refs.xTable.getCurrentRow();
          if (row) {
              this.shelfid = row.shelfid;
          }
          await this.btnQuery();
      },
      btnPrint() {},
      btnExport() {},
      /*-------------------------------其它-------------------------------*/
      libInit() {
          this.LoadSave();
          //先初始化架子类型再查询
          this.ddlShelfId();
      },
      //按钮 可用/禁用
      IsChange() {
          this.changeFlag = true;
      },
      //主键校验
      chkTxtShelfno() {
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP121",
                  method: "post",
                  data: {
                      action: "chktestgroupno",
                      testgroupno: this.bact_testgroup.testgroupno,
                  },
              })
              .then((res) => {
                  if (res == "1") {
                      this.$alert("试验组合代码重复，请重新填写！");
                      this.$refs.input0.focus();
                  }
              })
              .catch((err) => {
                  this.$alert(err);
              });
      },
      //焦点事件
      Autofocus(element) {
          if (element == "txtShelfTypeName") {
              this.$refs.selLibrID.focus();
          } else if (element == "txtShelfno") {
              this.$refs.selLibrID.blur();
              this.$refs.txtShelfdesc.focus();
          } else if (element == "txtShelfdesc") {
              this.btnSave();
          }
      },
      async setValueToTable(obj) {
          this.$nextTick(() => {
              console.log("123", obj);
              this.selRow.shelfid = obj.shelfid;
              this.selRow.librid = obj.librid;
              this.selRow.shelftypeno = obj.shelftypeno;
              this.selShelfidOptions.forEach((item) => {
                  if (obj.shelftypeno == parseInt(item.shelftypeno, 10)) {
                      this.selRow.shelftypename = item.shelftypename;
                  }
              });
              this.selRow.shelfno = obj.shelfno;
              this.selRow.shelfdesc = obj.shelfdesc;
              this.selRow.rownumber = obj.rownumber;
              this.selRow.colnumber = obj.colnumber;
              this.selRow.posnumber = obj.posnumber;
              this.selRow.locationid = obj.locationid;
              this.selRow.locationna = obj.locationna;
              this.selRow.usestatus = obj.usestatus;
              this.selRow.createuser = obj.createusername;
              this.selRow.createdt = obj.createdt;
              this.selRow.occupynumber = obj.occupynumber;
              this.selRow.freenumber = obj.freenumber;
          });
      },
      //样本架类型代码带出该样本架数据
      async GetShelfData() {
          if (this.detailData.shelftypeno != "") {
              try {
                  const res = await this.$tool.request({
                      url: "SMP/SMP1/SMP120",
                      method: "post",
                      data: {
                          action: "q_smp_shelftypeData",
                          librid: this.selCodeno,
                          shelftypeno: this.detailData.shelftypeno,
                      },
                  });
                  if (res) {
                      setTimeout(() => {
                          var tempShelfData = res[0];
                          this.detailData.rownumber = tempShelfData.rownumber;
                          this.detailData.colnumber = tempShelfData.colnumber;
                          this.detailData.posnumber = tempShelfData.posnumber;
                          this.detailData.librid = tempShelfData.librid;
                          this.itemKey++; //解决改数据源的值但是组件不刷新的问题
                          this.changeFlag = true;
                      }, 30);
                  }
              } catch (e) {
                  this.$message({
                      message: e.message,
                      type: "warning",
                      showClose: true,
                  });
              }
          } else {
              this.detailData = {};
          }
      },
      async ChkShelfNo() {
          try {
              const res = await this.$tool.request({
                  url: "SMP/SMP1/SMP121",
                  method: "post",
                  data: {
                      action: "chkshelfno",
                      shelfno: this.detailData.shelfno,
                      librid: this.selCodeno,
                  },
              });
              console.log("res", res);
              if (res == "1") {
                  this.$alert("架子编号已存在！", "提示", {
                      confirmButtonText: "确定",
                  });
                  this.$refs.txtShelfno.focus();
              }
          } catch (e) {
              this.$message({
                  message: e,
                  type: "warning",
                  showClose: true,
              });
          }
      },
      /*------------------------------Class------------------------------*/
      cellClassName({ columnIndex, row }) {
          if (columnIndex == "0" || columnIndex == "1") {
              if (row.flag == "1") {
                  //不能再次选择
                  return "col-orange";
              } else {
                  //新增的数据
                  return "";
              }
          } else {
              return "";
          }
      },
      RowClassName({ row, rowIndex }) {
          if (this.addData.indexOf(row.shelfid) > -1) {
              return "rm-new";
          }
          if (this.modData.indexOf(row.shelfid) > -1) {
              return "rm-modify";
          }
      },
  },
};
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  font-size: 12px;
  z-index: 99;
  overflow-x: auto;
  white-space: nowrap;
}
.pagetop .top-table {
  float: right;
}
.top-deal {
  right: 5px;
  position: absolute;
  top: 5px;
  display: flex;
  align-items: center;
}
.search-title {
  margin: 0 5px 0 20px;
  font-size: 12px;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  color: #606266;
  cursor: not-allowed;
}
::v-deep  .vxe-table .vxe-body--column.col-orange {
  padding: 3px;
  background-color: #f8f8f8;
}
::v-deep .el-input--suffix .el-input__inner {
  padding-right: 5px;
}
::v-deep .el-textarea__inner {
  padding: 3px 5px;
}
::v-deep  tr.vxe-body--row.row--current {
  color: #000000;
  font-weight: bold;
}

</style>
